<?php
/**
 * @copyright Copyright (c) 2022 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */
declare (strict_types=1);

namespace app\license\controller;

use app\api\BaseController;
use app\license\model\License;
use think\facade\Db;
use think\facade\View;

class Api extends BaseController
{

    public static $Status = [
        0 => '',
        1 => '施工',
        2 => '监理',
        3 => '设计',
        4 => '房地产',
        5 => '承装修饰',
    ];

    public function open_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.legal_name|a.company_name', 'like', '%' . $param['keywords'] . '%'];
                $where[] = ['a.delete_time', '=', 0];
                $model = new License();
                $list = $model->get_list($param, $where, $whereOr);
                return table_assign(0, '', $list);
            } else {
                return table_assign(0, '', ['data' => []]);
            }

        } else {
            return view();
        }
    }

    public function cert_index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            if (!empty($param['company_name'])) {
                $license_id = Db::name('license')->where('company_name', trim($param['company_name']))->value('id');
                $cert_ids = Db::name('license_cert')->where('license_id', $license_id)->column('id');
                $certs = Db::name('license_cert_cates')->whereIn('cert_id', $cert_ids)->select()->toArray();
                $str = '';
                $cert_types = [];
                foreach ($certs as $k => $v) {
                    $str .= $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'] . '|';
                    $cert_types[] = $v['cert_from_type'];
                }
                $cert_types = implode('|', array_unique($cert_types));
                return table_assign(0, '', ['data' => ['projects' => $str, 'cert_type' => $cert_types]]);
            } else {
                return table_assign(0, '', []);
            }
        } else {
            return view();
        }
    }


    public function index_data()
    {


        if (request()->isAjax()) {
            $param = get_params();
            $company_name = $param['company_name'];
            $info = Db::name('security_account')->where(['company_name' => $company_name])->where('delete_time', 0)->order('id desc')->find();

            $open_account_type = explode(',', $info['open_account_type']);
            $open_account_type_str = '';
            if (in_array('1', $open_account_type)) {
                $open_account_type_str .= '养老、';
            }
            if (in_array('2', $open_account_type)) {
                $open_account_type_str .= '失业、';
            }
            if (in_array('3', $open_account_type)) {
                $open_account_type_str .= '工伤、';
            }
            if (in_array('4', $open_account_type)) {
                $open_account_type_str .= '医疗、';
            }
            if (in_array('5', $open_account_type)) {
                $open_account_type_str .= '公积金、';
            }
            $info['open_account_type_str'] = $open_account_type_str;
            $data['data'] = $info;
            return table_assign(0, '', $data);
        }
    }
}
